/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import Controllers.utils.HibernateUtil;
import DAO.ProductsManager;
import Model.Produit;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Anthares
 */
public class Products extends BaseServlet {
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     * <p/>
     * @param request  servlet request
     * @param response servlet response
     * <p/>
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException      if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String locale = getLocaleString(request);
        String prodId = request.getParameter("id");
        if(prodId!=null){
            Produit prod = new ProductsManager(HibernateUtil.getSessionFactory(), locale).getProductByID(Integer.parseInt(prodId));
            request.setAttribute("product", prod);
            request.getRequestDispatcher("/product.jsp").forward(request, response);
        }else{
            request.getRequestDispatcher("/shop").forward(request, response);
        }
    }
}
